package cn.quevo.core.lang;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 自定义正则验证
 *
 * @param null
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018-03-09 16:18:05
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @return
 */
public final class RegularUtils {
    private static Logger log = LoggerFactory.getLogger(RegularUtils.class);
    /**
     * 手机号正则
     */
    private static String MOBILE = "^1[3|5|7|8][0-9]\\d{8}$";
    /**
     * 电话号码正则
     */
    private static String PHONE = "(^(\\d{3,4}-)?\\d{7,8}(-\\d{3,4})?)$";
    /**
     * 邮箱正则
     */
    private static String EMAIL = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
    /**
     * IP正则
     */
    private static String IP = "^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$/g";
    /**
     * 纯字母正则
     */
    private static String LETTER = "^[a-zA-Z]*";
    /**
     * 邮编正则
     */
    private static String ZIP_CODE = "^[1-9]\\d{5}";
    /**
     * 网址正则
     */
    private static String WEB_SITE = "^((https?):\\/\\/)?([a-z]([a-z0-9\\-]*[\\.])+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\\/[a-z0-9_\\-\\.~]+)*(\\/([a-z0-9_\\-\\.]*)(\\?[a-z0-9+_\\-\\.%=&]*)?)?(#[a-z][a-z0-9_]*)?$";
    /**
     * 仅包字母、下划线正则
     */
    private static String LETTER_UNDERLINE = "^[a-zA-Z]+(((\\_[a-zA-Z]+)*)|[a-zA-Z]*)$";
    /**
     * 仅包含数字、字母、下划线正则
     */
    private static String LETTER_NUM_UNDERLINE = "^([a-zA-Z]+|[1-9]+)(((\\_[a-zA-Z0-9]+)*)|[a-zA-Z0-9]*)$";
    /**
     * 仅包含英文和数字
     */
    private static String LETTER_NUM = "[a-zA-Z0-9]*$";
    private final static Map<String, String> regular = new LinkedHashMap<>();

    static {
        regular.put("MOBILE", MOBILE);
        regular.put("PHONE", PHONE);
        regular.put("EMAIL", EMAIL);
        regular.put("IP", IP);
        regular.put("LETTER", LETTER);
        regular.put("ZIP_CODE", ZIP_CODE);
        regular.put("WEB_SITE", WEB_SITE);
        regular.put("LETTER_UNDERLINE", LETTER_UNDERLINE);
        regular.put("LETTER_NUM_UNDERLINE", LETTER_NUM_UNDERLINE);
        regular.put("LETTER_NUM", LETTER_NUM);

        try (InputStream ins = RegularUtils.class.getResourceAsStream("regular.properties");) {
            Properties properties = new Properties();
            if (ins != null) {
                properties.load(ins);
                parseProperties(properties);
            }
            try (InputStream inst = RegularUtils.class.getResourceAsStream("/regular.properties");) {
                if (inst != null) {
                    properties.clear();
                    properties.load(inst);
                    parseProperties(properties);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 设置正则
     *
     * @param properties
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:18:17
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    private static void parseProperties(Properties properties) {
        Set<Map.Entry<Object, Object>> set = properties.entrySet();
        String key = "";
        for (Map.Entry<Object, Object> entry : set) {
            if (!StringUtils.isBlank(entry.getKey()) && !StringUtils.isBlank(entry.getValue())) {
                key = entry.getKey().toString().toUpperCase().trim();
                regular.put(key, entry.getValue().toString().trim());
                switch (key) {
                    case "MOBILE":
                        MOBILE = entry.getValue().toString();
                        break;
                    case "PHONE":
                        PHONE = entry.getValue().toString();
                        break;
                    case "EMAIL":
                        EMAIL = entry.getValue().toString();
                        break;
                    case "IP":
                        IP = entry.getValue().toString();
                        break;
                    case "LETTER":
                        LETTER = entry.getValue().toString();
                        break;
                    case "ZIP_CODE":
                        ZIP_CODE = entry.getValue().toString();
                        break;
                    case "WEB_SITE":
                        WEB_SITE = entry.getValue().toString();
                        break;
                    case "LETTER_UNDERLINE":
                        LETTER_UNDERLINE = entry.getValue().toString();
                        break;
                    case "LETTER_NUM_UNDERLINE":
                        LETTER_NUM_UNDERLINE = entry.getValue().toString();
                        break;
                    case "LETTER_NUM":
                        LETTER_NUM = entry.getValue().toString();
                        break;
                    default:
                        break;
                }
            }
        }
    }

    /**
     * 内置正则验证
     *
     * @param name
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:18:27
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean regular(final String name) {
        String key = name.toLowerCase();
        if (!regular.containsKey(key)) {
            return false;
        }
        return customRegular(key, regular.get(key));
    }

    /**
     * 自定义验证规则
     *
     * @param regValue
     * @param regExp
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:18:32
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean customRegular(final String regValue, final String regExp) {
        return Pattern.compile(regExp).matcher(regValue).find();
    }

    /**
     * 手机验证
     *
     * @param mobile
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:18:46
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean mobileCheck(final String mobile) {
        return customRegular(mobile, MOBILE);
    }

    /**
     * 电话验证
     *
     * @param phone
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:18:40
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean phoneCheck(final String phone) {
        return customRegular(phone, PHONE) ? true : mobileCheck(phone);
    }

    /**
     * 邮箱验证
     *
     * @param email
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:18:54
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean emailCheck(final String email) {
        return customRegular(email, EMAIL);
    }

    /**
     * IP验证
     *
     * @param ip
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:18:59
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean ipCheck(final String ip) {
        return customRegular(ip, IP);
    }

    /**
     * 字母验证(是否全部是字母)
     *
     * @param str
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:19:06
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean strCheck(final String str) {
        return customRegular(str, LETTER);
    }

    /**
     * 网址验证
     *
     * @param webStie
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:19:11
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean webStie(final String webStie) {
        return customRegular(webStie, WEB_SITE);
    }

    /**
     * 邮编验证
     *
     * @param zip
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:19:17
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean zip(final String zip) {
        return customRegular(zip, ZIP_CODE);
    }

    /**
     * 验证字符串是否仅仅包含英文和下划线[_]
     *
     * @param str
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:19:22
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean englishAndUnderline(final String str) {
        return customRegular(str, LETTER_UNDERLINE);
    }

    /**
     * 验证字符串是否仅仅包含英文数字
     *
     * @param str
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:19:29
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean englishOrNum(final String str) {
        return customRegular(str, LETTER_NUM);
    }

    /**
     * 验证字符串是否仅仅包含英文数字和下划线[_]
     *
     * @param str
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:19:35
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static boolean englishNumAndUnderline(final String str) {
        return customRegular(str, LETTER_NUM_UNDERLINE);
    }
}
